/*
 *
 */
package org.jiscinvolve.astrodabis.barberry.common.data;

import java.net.URI;
import java.util.Iterator;

import java.io.FileInputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.junit.Test;
import org.junit.After;
import org.junit.Before;
import static org.junit.Assert.*;

import org.springframework.beans.factory.annotation.Autowired;  

import org.springframework.core.task.TaskExecutor;

import org.jiscinvolve.astrodabis.barberry.common.core.Job;
import org.jiscinvolve.astrodabis.barberry.common.core.JobRunner;

import org.jiscinvolve.astrodabis.barberry.common.core.Tagged;
import org.jiscinvolve.astrodabis.barberry.common.core.Womble;
import org.jiscinvolve.astrodabis.barberry.common.core.Account;

import org.jiscinvolve.astrodabis.barberry.common.data.RowTag;
import org.jiscinvolve.astrodabis.barberry.common.data.JoinTag;

import org.jiscinvolve.astrodabis.barberry.common.data.TapTable;
import org.jiscinvolve.astrodabis.barberry.common.data.TapColumn;
import org.jiscinvolve.astrodabis.barberry.common.data.TapService;

import org.jiscinvolve.astrodabis.barberry.common.inport.votable.VOTableStarTableParser;
import org.jiscinvolve.astrodabis.barberry.common.inport.startable.TaggedStarTableParser;
import org.jiscinvolve.astrodabis.barberry.common.inport.startable.UnknownColumnException;

import org.jiscinvolve.astrodabis.barberry.hibernate.data.TapColumnEntity;
import org.jiscinvolve.astrodabis.barberry.hibernate.core.JobEntity;

/*
 *
 */
public class JobRunnerTestCase
extends DataTestBase
    {

    private static final String TEST_FILE  = "src/test/data/random/random-10000.xml" ;

    protected TapService service ;
    protected TapTable   tableone ;
    protected TapTable   tabletwo ;
    protected TapColumn  columnone ;
    protected TapColumn  columntwo ;

    @Autowired
    private TaskExecutor executor ;

    @Before
    public void before()
        {
        super.before();
        log.debug("JobRunnerTestCase.before()");

        service = womble().services().create(
            unique(
                URI.create(
                    "ivo://org.astrodabis.test/service/"
                    )
                ),
            "test-service"
            );

        tableone = service.tables().create(
            "test-table-one"
            );
        tabletwo = service.tables().create(
            "test-table-two"
            );
        columnone = tableone.columns().create(
            "test-column-one"
            );
        columntwo = tabletwo.columns().create(
            "test-column-two"
            );

        }


    @Test
    public void checkJobRuns()
    throws Exception
        {
        //
        // Create our job.
        Job job = account().jobs().create(
            new Job.Bean(
                "job-name",
                "job-text",
                columnone,
                columntwo           
                )
            );
        //
        // Add our data source.
        Tagged.Parser parser = new TaggedStarTableParser(
            job,
            new VOTableStarTableParser(
                new FileInputStream(
                    TEST_FILE
                    )
                )
            );
        //
        // Wrap it in a JobRunner
        Runnable runner = new JobRunner(
            parser
            );
        //
        // Run it in a new Thread.
        Thread thread = new Thread(
            runner
            );
        thread.start();
        thread.join();
        //
        // Check the job completed.
        assertEquals(
            Job.Status.COMPLETED,
            job.status()
            );

        }

// Check it handles bad data.
// - Number format exception.
// - Unmatched column names.

    @Test
    public void checkExceptionSameThread()
    throws Exception
        {
        Job job = account().jobs().create(
            new Job.Bean(
                "job-name",
                "job-text",
                columnone,
                columntwo           
                )
            );
        //
        // Parsing the data source without column aliases should fail
        // because it contains different column names.
        Runnable runner = new JobRunner(
            new TaggedStarTableParser(
                job,
                new VOTableStarTableParser(
                    new FileInputStream(
                        "src/test/data/join-test-003.xml"
                        )
                    )
                )
            );
        //
        // Run the job in our current Thread and
        // check it does NOT throw an exception.
        runner.run();
        //
        // Check the job failed.
        assertEquals(
            Job.Status.FAILED,
            job.status()
            );

/*
 * job.failText => class
 * job.failType => message
 *
 */
        }

    }
